import wx
from wx import aui
try:
    from agw import ribbon as RB
except ImportError: # if it's not there locally, try the wxPython lib.
    import wx.lib.agw.ribbon as RB



class notepad(wx.Frame):
    
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Notepad", size=(800,600))
        self.panel = wx.Panel(self)
        self.ribbon = RB.RibbonBar(self.panel, wx.ID_ANY)
        
        tabHome = RB.RibbonPage(self.ribbon, wx.ID_ANY, "Home", wx.NullBitmap )
        tabEdit = RB.RibbonPage(self.ribbon, wx.ID_ANY, "Edit", wx.NullBitmap )
        tabView = RB.RibbonPage(self.ribbon, wx.ID_ANY, "View", wx.NullBitmap )
        tabUpdate = RB.RibbonPage(self.ribbon, wx.ID_ANY, "Update", wx.NullBitmap )
        
        shapesL = RB.RibbonButtonBar(tabHome, wx.ID_ANY,size=(1,33) )
        shapesL.AddSimpleButton(wx.ID_ANY, "", wx.BitmapFromImage(wx.Image("image/empty1..png", wx.BITMAP_TYPE_ANY)) , "")
        
#        shapes.Enable(False)
#        toolbar = RB.RibbonToolBar(tabHome, wx.ID_ANY)
#        toolbar.AddSeparator()
#        temp = toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/empty1..png", wx.BITMAP_TYPE_ANY)), help_string="Connect")        
#        toolbar.Enable(False)
        
        toolbar = RB.RibbonToolBar(tabHome, wx.ID_ANY)
        temp = toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/connect.png", wx.BITMAP_TYPE_ANY)), help_string="Connect")        
        
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/new.png", wx.BITMAP_TYPE_ANY)), help_string="New")
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/disconnect.png", wx.BITMAP_TYPE_ANY)), help_string="Disconect")
        toolbar.Bind(wx.EVT_MOUSE_EVENTS,self.onShowHelp)
        
        shapes = RB.RibbonButtonBar(tabHome, wx.ID_ANY )
        shapes.AddSimpleButton(wx.ID_ANY, "", wx.BitmapFromImage(wx.Image("image/line.png", wx.BITMAP_TYPE_ANY)) , "")
        shapes.Enable(False)
        toolbar = RB.RibbonToolBar(tabHome, wx.ID_ANY)
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/copy.png", wx.BITMAP_TYPE_ANY)), help_string="Copy")
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/paste.png", wx.BITMAP_TYPE_ANY)), help_string="Paste")
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/find.png", wx.BITMAP_TYPE_ANY)), help_string="Find")
        toolbar.Bind(wx.EVT_MOUSE_EVENTS,self.onShowHelp)
        
        shapes = RB.RibbonButtonBar(tabHome, wx.ID_ANY )
        shapes.AddSimpleButton(wx.ID_ANY, "", wx.BitmapFromImage(wx.Image("image/line.png", wx.BITMAP_TYPE_ANY)) , "")
        shapes.Enable(False)
        toolbar = RB.RibbonToolBar(tabHome, wx.ID_ANY)
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/print.png", wx.BITMAP_TYPE_ANY)), help_string="Print")
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/colorchange.png", wx.BITMAP_TYPE_ANY)), help_string="Color")
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/font.png", wx.BITMAP_TYPE_ANY)), help_string="Font")
        toolbar.Bind(wx.EVT_MOUSE_EVENTS,self.onShowHelp)
        
        shapes = RB.RibbonButtonBar(tabHome, wx.ID_ANY )
        shapes.AddSimpleButton(wx.ID_ANY, "", wx.BitmapFromImage(wx.Image("image/line.png", wx.BITMAP_TYPE_ANY)) , "")
        shapes.Enable(False)
        toolbar = RB.RibbonToolBar(tabHome, wx.ID_ANY)
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/newwindow.png", wx.BITMAP_TYPE_ANY)), help_string="New")
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/send_file.png", wx.BITMAP_TYPE_ANY)), help_string="Send")
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/fullcreen.png", wx.BITMAP_TYPE_ANY)), help_string="Full screen")
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/lock.png", wx.BITMAP_TYPE_ANY)), help_string="Lock")
        toolbar.Bind(wx.EVT_MOUSE_EVENTS,self.onShowHelp)
        
        shapes = RB.RibbonButtonBar(tabHome, wx.ID_ANY )
        shapes.AddSimpleButton(wx.ID_ANY, "", wx.BitmapFromImage(wx.Image("image/line.png", wx.BITMAP_TYPE_ANY)) , "a")
        shapes.Enable(False)
        toolbar = RB.RibbonToolBar(tabHome, wx.ID_ANY)
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/kb.png", wx.BITMAP_TYPE_ANY)), help_string="Keyboard")
        toolbar.AddSeparator()
        toolbar.AddTool(wx.ID_ANY, wx.BitmapFromImage(wx.Image("image/seting.png", wx.BITMAP_TYPE_ANY)), help_string="Option")
        
        toolbar.SetRows(1,1)
        toolbar.Bind(wx.EVT_MOUSE_EVENTS,self.onShowHelp)
        
        temp.position = wx.Point(20,20)
        self.ribbon.Realize()
        shapesL.SetSize((3,33))
        shapesL.Enable(False)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.ribbon, 0, wx.EXPAND)
        
        self.nb = aui.AuiNotebook(self.panel,size=(800,600))
        
        self.nb.SetTabCtrlHeight(30)
        
        page = wx.TextCtrl(self.nb, -1, "sdfsd", style=wx.TE_MULTILINE)
        self.nb.AddPage(page, "Welcome")
        self.nb.AddPage(wx.StaticText(self,-1,""), "",bitmap = wx.Bitmap("image/plus2.png", wx.BITMAP_TYPE_ANY))
        self.nb.Bind(aui.EVT_AUINOTEBOOK_PAGE_CHANGING, self.chageTab)
#        self.nb.Bind(aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.chageTab)
#        self.nb.Bind(aui.EVT_AUINOTEBOOK_PAGE_CLOSED, self.tabClose)
        
        sizer.Add(self.nb , 1, wx.EXPAND)
        self.nb.SetWindowStyle(aui.AUI_NB_CLOSE_ON_ACTIVE_TAB);
        
        self.panel.SetSizer(sizer)
    
    def onShowHelp(self, event): 
        if event.GetEventObject()._hover_tool: 
            event.GetEventObject().SetToolTipString(event.GetEventObject()._hover_tool.help_string)
        event.Skip()
        
    def chageTab(self,event):
        print "chagedTab", self.nb.GetPageCount(),event.GetSelection(),self.nb.GetSelection()
        if(event.GetSelection()==0):
            self.nb.SetWindowStyle(aui.AUI_NB_TOP | aui.AUI_NB_TAB_SPLIT | aui.AUI_NB_TAB_MOVE | aui.AUI_NB_SCROLL_BUTTONS);
            event.Skip()
        else:
            if self.nb.GetPageCount() == event.GetSelection()+1:
#                a, b = event
#                self.nb.DeletePage(b)
#                self.openTab()
#                page = self.nb.GetPage(self.nb.GetSelection())
                print 'aaa'
#                page.SetToolTipString("adsfad")
                print 'aaa'
                
                self.menupopup = wx.Menu()
                
                idOpen = wx.NewId()
                item = wx.MenuItem(self.menupopup, idOpen, "Open")
                self.Bind(wx.EVT_MENU, self.openTab, id = idOpen)
                self.menupopup.AppendItem(item)
                
                for index in range(self.nb.GetPageCount()-1):
                    pageText = self.nb.GetPageText(index)
                    idOpen = wx.NewId()
                    item = wx.MenuItem(self.menupopup, idOpen, pageText)
                    self.Bind(wx.EVT_MENU, self.openTab, id = idOpen)
                    self.menupopup.AppendItem(item)
                #        bmp = wx.ArtProvider.GetBitmap(wx.ART_COPY, wx.ART_MENU, (24,24))
                #        item.SetBitmap(bmp)
                
                
                x,y  =self.GetPosition()
                x1,y1 = wx.GetMousePosition()
                self.PopupMenu(self.menupopup,(x1 - x - 8, 94))
                self.menupopup.Destroy()
                
                #self.nb.SetSelection(event.GetSelection())
            else:
                self.nb.SetWindowStyle(aui.AUI_NB_DEFAULT_STYLE)
                event.Skip()
    
    def openTab(self,event):
        panel = wx.Panel(self.nb,-1)
        grid1 =  rcs.RowColSizer()
        txt = wx.TextCtrl(panel, -1, "This is page " ,
                               style=wx.TE_MULTILINE)
        saveBnt = wx.Button(panel, label = "Save")
        
        grid1.Add(txt,row=1, col=1,flag=wx.EXPAND)
        grid1.Add(saveBnt,row=2, col=1)        
        panel.SetSizer(grid1) 
        self.nb.InsertPage(self.nb.GetPageCount()-1,panel,"new")
    
         
        
app = wx.PySimpleApp()
notepad().Show()
app.MainLoop()